import {requestForm8082} from './requestForm8082.ts';
import axios, {post} from "axios";
import {Ref, ref} from "vue";
import {requestJson, requestJson8084} from "./requestJson8084.ts";
import {requestJson8082} from "./requestJson8082.ts";


import {onMounted, ref} from "vue";
import {taskImage} from "../store/taskStore.ts";
import html2canvas from "html2canvas";

export let ws = ref()
export let messageList = ref([])
export let sendMessage = ref('')
export let returnMessage = ref('')
export function webSocketInit(){
    const webSocketUrl = 'ws://192.168.152.109:8085/trace-server/ws'
    ws.value = new WebSocket(webSocketUrl);
    console.log(ws.value)
    ws.value.onopen = () => {
        console.log("连接成功");
    };
    //后端设置心跳，会每间隔一定时间，触发一次，根据内容变化处理逻辑
    ws.value.onmessage = (e: any) => {
        returnMessage.value = e.data
        // console.log(e.data, "广播返回的消息");
        //后端约定了，如果返回字符串“UPDATE”，就更新表格
        if (e.data === "UPDATE") {
            //getTable();
        }
        return e.data
    };
    ws.value.onerror = () => {
        console.log("连接错误");
        //断连后每5秒重连一次
        setTimeout(() => {
            initWebSocket();
        }, 5000);
    };

}

export function handleSendButton() {
    const message = sendMessage.value
    ws.value.send(JSON.stringify({
        message,
        time: new Date().getTime()
    }))
    sendMessage.value = ''
}
export function cancelWebSocket(){
    ws.value.close()
    console.log("关闭websocket连接");
    // console.log("关闭连接,重新开始连接");
    // webSocketInit();
}
export function handleLoginButton(){
    webSocketInit()
    // console.log("关闭连接,重新开始连接");
    // webSocketInit();
}

